package com.xintai.sparepart.spareandpart.controller;

import java.util.List;

import com.xintai.common.constant.Constants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xintai.common.annotation.Log;
import com.xintai.common.core.controller.BaseController;
import com.xintai.common.core.domain.AjaxResult;
import com.xintai.common.enums.BusinessType;
import com.xintai.sparepart.spareandpart.domain.BeiCangku;
import com.xintai.sparepart.spareandpart.service.IBeiCangkuService;
import com.xintai.common.utils.poi.ExcelUtil;
import com.xintai.common.core.page.TableDataInfo;

/**
 * 仓库Controller
 *
 * @author xintai
 * @date 2020-12-22
 */
@RestController
@RequestMapping("/sparepart/cangku")
public class BeiCangkuController extends BaseController
{
    @Autowired
    private IBeiCangkuService beiCangkuService;

    /**
     * 查询仓库列表
     */
    @PreAuthorize("@ss.hasPermi('sparepart:cangku:list')")
    @GetMapping("/list")
    public TableDataInfo list(BeiCangku beiCangku)
    {
        beiCangku.setCangkuIsdel(Constants.SUCCESS);
        List<BeiCangku> list = beiCangkuService.selectBeiCangkuList(beiCangku);
        return getDataTable(list);
    }

    /**
     * 导出仓库列表
     */
    @PreAuthorize("@ss.hasPermi('sparepart:cangku:export')")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BeiCangku beiCangku)
    {
        List<BeiCangku> list = beiCangkuService.selectBeiCangkuList(beiCangku);
        ExcelUtil<BeiCangku> util = new ExcelUtil<BeiCangku>(BeiCangku.class);
        return util.exportExcel(list, "cangku");
    }

    /**
     * 获取仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('sparepart:cangku:query')")
    @GetMapping(value = "/{cangkuId}")
    public AjaxResult getInfo(@PathVariable("cangkuId") Long cangkuId)
    {
        return AjaxResult.success(beiCangkuService.selectBeiCangkuById(cangkuId));
    }

    /**
     * 新增仓库
     */
    @PreAuthorize("@ss.hasPermi('sparepart:cangku:add')")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BeiCangku beiCangku)
    {
        if (beiCangkuService.selectNamesake(beiCangku.getCangkuName())){
            return new AjaxResult().error("新增失败 '"+beiCangku.getCangkuName()+"' 已存在");
        }
        return toAjax(beiCangkuService.insertBeiCangku(beiCangku));
    }

    /**
     * 修改仓库
     */
    @PreAuthorize("@ss.hasPermi('sparepart:cangku:edit')")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BeiCangku beiCangku)
    {

        return toAjax(beiCangkuService.updateBeiCangku(beiCangku));
    }

    /**
     * 删除仓库
     */
    @PreAuthorize("@ss.hasPermi('sparepart:cangku:remove')")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
	@DeleteMapping("/delcangku")
    public AjaxResult remove(@RequestBody BeiCangku beiCangku)
    {
        if (beiCangkuService.selectkuwei(beiCangku.getCangkuId())){
            return new AjaxResult().error("删除失败 '"+beiCangku.getCangkuName()+"' 存在库位");
        }
        return toAjax(beiCangkuService.deleteBeiCangkuById(beiCangku.getCangkuId()));
    }

}
